package com.example.redis.domain;


import cn.hutool.http.HttpStatus;
import com.example.redis.common.enums.ResponseCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author ruoyi
 */
@ApiModel(value = "ResponseVO", description = "接口响应对象，主要包含状态码、响应信息以及响应内容")
@Data
public class ResultVO<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = HttpStatus.HTTP_OK;

    /**
     * 失败
     */
    public static final int FAIL = HttpStatus.HTTP_INTERNAL_ERROR;

    @ApiModelProperty(value = "状态码", name = "code", example = "200")
    private int code;

    @ApiModelProperty(value = "响应信息", name = "message", example = "success")
    private String msg;

    @ApiModelProperty(value = "响应内容")
    private T data;

    public static <T> ResultVO<T> ok() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> ResultVO<T> ok(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> ResultVO<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> ResultVO<T> ok(ResponseCode responseCode, T data) {
        return restResult(data, responseCode.getCode(), responseCode.getMessage());
    }

    public static <T> ResultVO<T> fail() {
        return restResult(null, FAIL, "操作失败");
    }

    public static <T> ResultVO<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> ResultVO<T> fail(T data) {
        return restResult(data, FAIL, "操作失败");
    }

    public static <T> ResultVO<T> fail(ResponseCode responseCode) {
        return restResult(null, responseCode.getCode(), responseCode.getMessage());
    }
    public static <T> ResultVO<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> ResultVO<T> restResult(T data, int code, String msg) {
        ResultVO<T> apiResult = new ResultVO<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public static <T> Boolean isError(ResultVO<T> ret) {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(ResultVO<T> ret) {
        return ResultVO.SUCCESS == ret.getCode();
    }
}
